package com.xin.uploadanddownload.test;





import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class URLCode {
    public static void main1(String[] args) throws UnsupportedEncodingException {
        /*
             正常 中国
             说明 中国 通过URL以utf-8编码是这个 %E4%B8%AD%E5%9B%BD
             中国 = utf8 = %E4%B8%AD%E5%9B%BD
         */
        String decode = URLDecoder.decode("%E4%B8%AD%E5%9B%BD", "UTF-8");
        /*
              乱码 �й�
              中国 = GB2312 =  %D6%D0%B9%FA
              �й� = utf8 = %D6%D0%B9%FA
              �й� = 十六进制 = D6 D0 B9 FA
         */
        String decode2 = URLDecoder.decode("%D6%D0%B9%FA", "UTF-8");
        String decode3 = URLDecoder.decode("%D6%D0%B9%FA", "GB2312");
        System.out.println(decode3);
        System.out.println(decode2);
        System.out.println(decode);
    }

    public static void main3(String[] args) throws UnsupportedEncodingException {
        String name="中国";
//        byte[] bytes = name.getBytes("UTF-8");
        byte[] bytes = name.getBytes("GBK");
        String string = new String(bytes);
        System.out.println(string);
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        String test="%U81F4%U8FDCA8-N%U534F%U540C%U529E%U516C%U7CFB%U7EDF%U7528%U6237%U64CD%U4F5C%U624B%U518C";
        String replace = test.replace('U', 'u');
        System.out.println(replace);
        String replace1 = replace.replace('%', '\\');
        System.out.println(replace1);
        System.out.println(new String(replace1.getBytes("UTF-8"),"unicode"));

    }
    public static void mainq(String[] args) {
        try {
            String encoded = "%U81F4";
            String decoded = URLDecoder.decode(encoded, StandardCharsets.UTF_8.name());
            System.out.println(decoded);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        String unicode = "\\u81F4\\u8FDCA8-N\\u534F\\u540C\\u529E\\u516C\\u7CFB\\u7EDF\\u7528\\u6237\\u64CD\\u4F5C\\u624B\\u518C";
        String test="%U81F4%U8FDCA8-N%U534F%U540C%U529E%U516C%U7CFB%U7EDF%U7528%U6237%U64CD%U4F5C%U624B%U518C";

        String string = new String(test.getBytes("GBK"),"GBK");
        System.out.println(string);
//        String str = unicodeToString(test);
//        System.out.println(str);
//        int i = 33268;
//        System.out.println( (char)i);
    }

    public static String unicodeToString(String unicode) {
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (index < unicode.length()) {
            if (unicode.charAt(index) == '%' && unicode.charAt(index + 1) == 'U') {
                String hex = unicode.substring(index + 2, index + 6);
                int codePoint = Integer.parseInt(hex, 16);
//                System.out.println(codePoint);
//                System.out.println((char) codePoint);
                sb.append((char) codePoint);
                index += 6;
            } else {
                sb.append(unicode.charAt(index));
                index++;
            }
        }
        return sb.toString();
    }

}
